<?php
class Line_order_profit_model extends CI_Model
{
    private $table   = 'line_order_profit';
    private $table_1 = 'sales_order';
    private $table_2 = 'line_order_product';
    private $table_3 = 'line_order_refund';
    private $table_4 = 'line_base';
    private $table_5 = 'user';
    
    public function insertIntoOrderProfit($order_id, $orderProfit)
    {
        $data = array();
        foreach ($orderProfit as $key=>$value) {
            $data[$key]             = $value;
            $data[$key]['order_id'] = $order_id;
        }
        return $this->db->insert_batch($this->table, $data);
    }
    
    /**
     * 获取订单分润数据
     * @param unknown $order_id
     */
    public function findByOrderId($order_id)
    {
        $this->db->where('order_id', $order_id);
        return $this->db->get($this->table);
    }
    
    public function findByOrderPrivider($order_id)
    {
        $this->db->where('order_id', $order_id);
        $this->db->where('as', 1);
        $this->db->where('account_type', 1);
        $this->db->where('bit_trade !=', UTID_BEIZHU);
        $this->db->where('bit_trade & '.UTID_PROVIDER.'=', UTID_PROVIDER);
        return $this->db->get($this->table);
    }
    
    public function updatePaymentAdvanceMoney($order_id, $account, $supplyUid)
    {
        $this->db->set('account', 'account-'.$account, false);
        $this->db->where('order_id', $order_id);
        $this->db->where('uid', $supplyUid);
        $this->db->where('as', 1);
        $this->db->where('account_type', 1);
        $this->db->where('bit_trade !=', UTID_BEIZHU);
        $this->db->where('bit_trade & '.UTID_PROVIDER.'=', UTID_PROVIDER);
        return $this->db->update($this->table);
    }
    
    /**
     * @descripte   分润
     * @Author xiumao
     * @date 2016/6/28 0028 下午 3:41
     */
    public function profit_total($params = array())
    {
        $this->db->join($this->table_1 . ' as sales_order', 'sales_order.order_id = line_order_profit.order_id');
        $this->db->join($this->table_2 . ' as line_order_product', 'line_order_product.order_id = line_order_profit.order_id');
        $this->db->join($this->table_4 . ' as line_base', 'line_base.id = line_order_product.line_id');

        if ( !empty($params['uid']) ) {
            $this->db->where('line_order_profit.uid', $params['uid']);
        }
        if ( !empty($params['payer_uid']) ) {
            $this->db->where('sales_order.uid', $params['payer_uid']);
        }
        if ( !empty($params['order_id']) ) {
            $this->db->where('sales_order.order_id', $params['order_id']);
        }
        if ( !empty($params['startTime']) ) {
            $this->db->where('sales_order.created_at >=', $params['startTime'] . ' 00:00:00');
        }
        if ( !empty($params['endTime']) ) {
            $this->db->where('sales_order.created_at <=', $params['endTime'] . ' 23:59:59');
        }
        if ( !empty($params['status']) && $params['status'] == 2 ) {
            $this->db->where("sales_order.order_state", 3);
        } else if ( !empty($params['status']) && $params['status'] == 1 ) {
            $this->db->where("sales_order.order_state !=", 3);
        }
        $this->db->where("sales_order.order_state >", 1);
        $this->db->where("sales_order.order_state !=", 4);
        $this->db->where("sales_order.order_status !=", 3);
        $this->db->where("sales_order.order_status !=", 6);


        return $this->db->count_all_results($this->table);
    }

    /**
     * @descripte   分润
     * @Author xiumao
     * @date 2016/6/28 0028 下午 3:55
     */
    public function profit_page_list($page_num, $num, $params = array())
    {
        $this->db->select('line_base.line_travel_type,
                        sales_order.user_name,
                        sales_order.created_at,
                        sales_order.order_state,
                        line_order_profit.account,
                        line_order_profit.order_id,
                        line_order_product.line_name,
                        line_order_product.amount_original_total');
        $this->db->join($this->table_1 . ' as sales_order', 'sales_order.order_id = line_order_profit.order_id');
        $this->db->join($this->table_2 . ' as line_order_product', 'line_order_product.order_id = line_order_profit.order_id');
        $this->db->join($this->table_4 . ' as line_base', 'line_base.id = line_order_product.line_id');

        if ( !empty($params['uid']) ) {
            $this->db->where('line_order_profit.uid', $params['uid']);
        }
        if ( !empty($params['payer_uid']) ) {
            $this->db->where('sales_order.uid', $params['payer_uid']);
        }
        if ( !empty($params['order_id']) ) {
            $this->db->where('sales_order.order_id', $params['order_id']);
        }
        if ( !empty($params['startTime']) ) {
            $this->db->where('sales_order.created_at >=', $params['startTime'] . ' 00:00:00');
        }
        if ( !empty($params['endTime']) ) {
            $this->db->where('sales_order.created_at <=', $params['endTime'] . ' 23:59:59');
        }
        if ( !empty($params['status']) && $params['status'] == 2 ) {
            $this->db->where("sales_order.order_state", 3);
        } else if ( !empty($params['status']) && $params['status'] == 1 ) {
            $this->db->where("sales_order.order_state !=", 3);
        }
        $this->db->where("sales_order.order_state >", 1);//未付款 不显示
        $this->db->where("sales_order.order_state !=", 4);//退票 不显示
        $this->db->where("sales_order.order_status !=", 3);//取消订单 不显示
        $this->db->where("sales_order.order_status !=", 6);//退款订单 不显示

        $this->db->from($this->table);
        $this->db->order_by('sales_order.created_at', 'DESC');
        $this->db->limit($page_num, $num);

        return $this->db->get();
    }

    /**
     * @descripte   分润已到账收入
     * @Author xiumao
     * @date 2016/6/28 0028 下午 4:55
     */
    public function profit_account($params = array())
    {
        $this->db->select_sum('account');
        $this->db->join($this->table_1 . ' as sales_order', 'sales_order.order_id = line_order_profit.order_id');
        $this->db->join($this->table_2 . ' as line_order_product', 'line_order_product.order_id = line_order_profit.order_id');
        $this->db->join($this->table_4 . ' as line_base', 'line_base.id = line_order_product.line_id');

        if ( !empty($params['uid']) ) {
            $this->db->where('line_order_profit.uid', $params['uid']);
        }
        if ( !empty($params['payer_uid']) ) {
            $this->db->where('sales_order.uid', $params['payer_uid']);
        }
        if ( !empty($params['order_id']) ) {
            $this->db->where('sales_order.order_id', $params['order_id']);
        }
        if ( !empty($params['startTime']) ) {
            $this->db->where('sales_order.created_at >=', $params['startTime'] . ' 00:00:00');
        }
        if ( !empty($params['endTime']) ) {
            $this->db->where('sales_order.created_at <=', $params['endTime'] . ' 23:59:59');
        }
        if ( !empty($params['status']) && $params['status'] == 2 ) {
            $this->db->where("sales_order.order_state", 3);
        } else if ( !empty($params['status']) && $params['status'] == 1 ) {
            $this->db->where("sales_order.order_state !=", 3);
        }
        $this->db->where("sales_order.order_state >", 1);
        $this->db->where("sales_order.order_state !=", 4);
        $this->db->where("sales_order.order_status !=", 3);
        $this->db->where("sales_order.order_status !=", 6);
        $this->db->from($this->table);

        return $this->db->get();
    }
    
}
